//
//  AppDelegate.m
//  idoctor
//
//  Created by Mr Lee on 2017/6/1.
//  Copyright © 2017年 Mr Lee. All rights reserved.
//

#import "AppDelegate.h"

#import "SICKViewController.h"
#import "NEWSViewController.h"
#import "MEDViewController.h"
#import "CHEViewController.h"

@interface AppDelegate ()<UITabBarControllerDelegate>

@end

@implementation AppDelegate


//标签栏控制器
-(void)tabBarController
{
    
    //创建单例对象
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    
    int index = [[ud objectForKey:@"index"] intValue];
    
    //创建标签栏视图控制器
    UITabBarController *tab = [[UITabBarController alloc] init];
    //设置代理 建立委托代理关系
    //tab.delegate = self;
    
    
    
    NEWSViewController *newsVc = [[NEWSViewController alloc] init];
    //设置视图控制器的标题
    newsVc.title = @"资讯";
    
    //设置视图控制器的标签栏按钮 tabBarItem
    //initWithTabBarSystemItem 用系统给的方式初始化
    newsVc.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"资讯" image:[[UIImage imageNamed:@"1.jpeg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"1.jpeg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
;
    SICKViewController *sickVc = [[SICKViewController alloc] init];
    //用标题和两张图片初始化
    sickVc.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"疾病" image:[[UIImage imageNamed:@"1.jpeg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"1.jpeg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    sickVc.tabBarItem.tag = 1;
    sickVc.title = @"疾病";
    
    MEDViewController *medVc = [[MEDViewController alloc] init];
    
    //用文字和图片初始化
    medVc.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"药品" image:[[UIImage imageNamed:@"1.jpeg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"1.jpeg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    //设置标题
    medVc.title = @"药品";
    
    
    CHEViewController *cheVc = [[CHEViewController alloc] init];
    //用文字初始化 按钮不显示
    cheVc.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"药店" image:[[UIImage imageNamed:@"1.jpeg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"1.jpeg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    cheVc.title = @"药店";
   
    //创建导航控制器 分别把四个视图控制器设置成为导航控制器的根视图控制器，这样我们点击那个导航控制器。就是显示的哪个视图控制器
    UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:newsVc];
    UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:sickVc];
    UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:medVc];
    UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:cheVc];
    
    
    //把导航控制器给标签栏控制器 标签栏控制器的viewControllers数组里有几个导航控制器，就有几个模块
    tab.viewControllers = @[nav1,nav2,nav3,nav4];
    
    //设置tabBar选择下标 默认是0
    tab.selectedIndex = index;
    //设置tabBar选个哪个视图控制器
    tab.selectedViewController = tab.viewControllers[index];
    
    _window.rootViewController = tab;
}

//已经选择某个视图控制器
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    //创建一个单例
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    
    //存数据
    [ud setObject:[NSNumber numberWithInteger:tabBarController.selectedIndex] forKey:@"index"];
    
    //写入磁盘
    [ud synchronize];
    
    NSLog(@"已经选择某个视图控制器%lu",(unsigned long)tabBarController.selectedIndex);
}



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    _window.backgroundColor = [UIColor whiteColor];
    [self tabBarController];
    
    [_window makeKeyAndVisible];
    
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
